import Vue from 'vue'
import VueRouter from 'vue-router'

//导入组件
import Movie from '@/views/Movie'
import Cinema from '@/views/Cinema'
import My from '@/views/My'
import News from '@/views/News'
import nowplaying from '../views/Movie/nowplaying.vue'
import moviedetail from '@/views/Movie/moviedetail'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/movie'
  },
  {
    path: '/movie',
    component: Movie,
    children: [
      // {
      //   path: 'nowplaying',
      //   component: nowplaying
      // },
      // {
      //   path:'comingsoon',
      //   component: () => import('@/views/Movie/comingsoon'),
      // },
      {
        path: 'moviedetail',
        component: moviedetail
      }
    ]
  },
  {
    path: '/cinema',
    component: () => import('@/views/Cinema/cinema'),
    redirect: '/cinema/index',
    children: [
      {
        path: 'index',
        component: Cinema
      },
      {
        path: 'film',
        component: () => import('@/views/Cinema/film')
      }
    ]
  },
  {
    path: '/my',
    component: My,
    redirect: '/my/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/My/Myindex')
      },
      {
        path: 'login',
        component: () => import('@/views/My/login')
      },
      {
        path: 'register',
        component: () => import('@/views/My/register')
      }
    ]
  },
  {
    path: '/news',
    component: News
  },
  {
    path: '/city',
    component: () => import('@/views/City')
  },
  // {
  //   path: '*',
  //   redirect: '/movie'
  // }
]

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to, from);
  let arr = ['/movie', '/cinema/index', '/news', '/my/index']
  let index = arr.indexOf(to.path)
  // console.log(arr, to.path, index);
  store.commit('changeActive', index)
  next()
})

export default router
